from pynput import keyboard
from pynput import mouse
import time
import threading
import queue
q = queue.Queue() #task 0,1,2,3,4  left right press release

alt_pressed = False
shift_pressed = False
ctrl_pressed = False
key_pressed_when_shift = False
is_pressed = False
def do_task():
    while True:
        task = q.get()
        print('got task', task)
        time.sleep(0.5)
        
        if task == 0:
            mouse.Controller().click(mouse.Button.left,1)
            mouse.Controller().release(mouse.Button.left)            
            
        if task == 1:
            mouse.Controller().click(mouse.Button.right,1)
            
t = threading.Thread(target=do_task)
t.setDaemon(True)
t.start()

def on_press(key):
    print('pre', key)
    global alt_pressed, shift_pressed, ctrl_pressed, key_pressed_when_shift, is_pressed
    
    if 'alt' in str(key):
        alt_pressed = True
    
    if 'shift' in str(key):
        shift_pressed = True
        key_pressed_when_shift = False        
        
    if 'ctrl' in str(key):
        ctrl_pressed = True
    
    if 'shift' not in str(key):
        key_pressed_when_shift = True
        
    if '<252>' not in str(key):
        return
    
    if not alt_pressed and not ctrl_pressed and not shift_pressed:
        mouse.Controller().click(mouse.Button.left,1)                
        return
    
    if ctrl_pressed:
        if not is_pressed:
            mouse.Controller().press(mouse.Button.left)
            print('p')
            is_pressed = True
        else:
            mouse.Controller().release(mouse.Button.left)
            is_pressed = False
        return
    if alt_pressed:
        mouse.Controller().click(mouse.Button.right,1)                
        
        
def on_release(key):
    print('rele', key)
    
    global is_pressed, alt_pressed
    global shift_pressed, ctrl_pressed
    
    if 'ctrl' in str(key):
        ctrl_pressed = False
        
    if 'shift' in str(key):
        shift_pressed = False
        print(is_pressed, key_pressed_when_shift)
        if is_pressed and not key_pressed_when_shift:
            print('hit')
            mouse.Controller().release(mouse.Button.left)
            is_pressed = False
            
        
    if 'alt' in str(key):
        alt_pressed = False
    
   
listener = keyboard.Listener(
    on_release=on_release,
    on_press=on_press,
    
)
listener.start()
import time
while True:
    time.sleep(100)